package com.flong.selenium;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.UnexpectedAlertBehaviour;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.flong.common.IoUtil;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

@SuppressWarnings("all")
public class MeiTuanTest7 {

	public static WebDriver driver;

	static {
		System.setProperty("webdriver.ie.driver", "d:/Devsoft/IEDriverServer.exe");
		// driver = new ChromeDriver();
		DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();
		ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
		ieCapabilities.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR,UnexpectedAlertBehaviour.ACCEPT);
		driver = new InternetExplorerDriver(ieCapabilities); // 问题3解决方法
		driver.manage().timeouts().implicitlyWait(1000, TimeUnit.SECONDS);// SECONDS是秒的意思
	}

	public static void main(String[] args) throws IOException, Exception {
		String path = "d:/pc_task/test.xls";	
		
		Workbook book = Workbook.getWorkbook(new File(path));
		Sheet sheet = book.getSheet(1);
		int rows = sheet.getRows();
		String result = "";
		for (int i = 0; i <rows; i++) {
			Cell cell = sheet.getCell(4, i);
			String text = cell.getContents().trim().replace("", "");
			if(text!=null && !"".equals(text)){
				 driver.navigate().to(text);
				 //Thread.sleep(1000);
				 //WebDriverWait wait = new WebDriverWait(driver,1);//等待1s
				 if(isAlertPresent()){
					 driver.switchTo().alert().accept();
				 }
				 WebElement contentInfo = driver.findElement(By.cssSelector("div[class='biz-info__content']"));
				 String shopname = driver.findElement(By.className("biz-info__title")).getText();
				 String address = contentInfo.findElement(By.cssSelector("div[class='biz-item field-group']")).getAttribute("title");
				 //如果找出样式biz-item等于4获取相关信息
				 List<WebElement> bizItems = contentInfo.findElements(By.className("biz-item"));
				 if(bizItems!=null && bizItems.size()==4){
					 String tel = contentInfo.findElement(By.xpath("//div[4][@class='biz-item']")).getText().replace("电话：", "");
					 if(tel!=null && !"".equals(tel))
						 result+=shopname+"="+address+"="+tel+"\n";
					 System.out.println(result);
				 }
			}
		}
		IoUtil.save("d:/pc_task/wh005.txt", result);
		System.err.println("success~~~!");
		book.close();
		driver.close();
		driver.quit();
	
	}

	/**
	 * 判断是否存在alert
	 * @return
	 */
	protected static boolean isAlertPresent() {
        try {
          driver.switchTo().alert();
          return true;
        } catch (NoAlertPresentException e) {
          return false;
        }
      }
	
	/**
	 * 是否存在元素
	 * @param by
	 * @return
	 */
	private static boolean isElementPresent(By by){
	      try{
	          driver.findElement(by);
	          return true;
	      }catch(Exception e){
	          return false;
	      }

	  }
}